package com.mcc.app.utils;

import cn.hutool.poi.excel.ExcelWriter;
import cn.hutool.poi.excel.cell.CellLocation;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CellType;

public class MyExcelUtil {

    public static void writeCellValue(ExcelWriter exportWriter, String ref, Object value) {
        exportWriter.writeCellValue(ref, value);
    }

//    public static void writeCellValue(ExcelWriter exportWriter, String ref, Object value, CellStyle cellStyle) {
//        exportWriter.writeCellValue(ref, value).getOrCreateCell(ref).setCellStyle(cellStyle);
//    }

    public static void writeCellValue(ExcelWriter exportWriter, String ref, String value, Boolean isFormula) {
        exportWriter.getOrCreateCell(ref).setCellFormula(value);
    }

    public static void writeCellValue(ExcelWriter exportWriter, String ref, int rowLength, Object value, CellStyle cellStyle) {
        writeCellValue(exportWriter, ref, rowLength, 1, value, cellStyle);
    }

    public static void writeCellValue(ExcelWriter exportWriter, String ref, int rowLength, int colLength, Object value, CellStyle cellStyle) {
        CellLocation cellLocation = cn.hutool.poi.excel.ExcelUtil.toLocation(ref);
        int firstRowIndex = cellLocation.getY();
        int lastRowIndex = firstRowIndex + rowLength - 1;
        int firstColIndex = cellLocation.getX();
        int lastColIndex = firstColIndex + colLength - 1;
        exportWriter.merge(firstRowIndex, lastRowIndex, firstColIndex, lastColIndex, value, false);
        for (int i = firstRowIndex; i <= lastRowIndex; i++) {
            for (int j = firstColIndex; j <= lastColIndex; j++) {
                exportWriter.getOrCreateCell(j, i).setCellStyle(cellStyle);
            }
        }
    }
}
